package com.fit.pages.organization;

import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.services.PropertyAccess;

import com.fit.entities.ContactPerson;
import com.fit.entities.Organization;
import com.fit.model.gsm.GenericSelectModel;
import com.fit.pages.Confirmation;
import com.fit.dao.contactPerson.ContactPersonDAO;
import com.fit.dao.organization.OrganizationDAO;
public class Edit {

	@Property
	@Persist
	private Organization organization;
	@Inject
	private OrganizationDAO organizationDAO;
	@InjectPage
	private AddOrganization addOrganization;
	@InjectPage
	private Confirmation confirmation;
	
	@Inject
	private PropertyAccess access;
	
	@Inject
	private ContactPersonDAO contactPersonDAO;
	
	@Property
	private ContactPerson contactPerson;

	private GenericSelectModel<ContactPerson> contactPersons;
 
	public GenericSelectModel<ContactPerson> getContactPersons() {
		contactPersons = new GenericSelectModel<ContactPerson>(
				contactPersonDAO.findAll(), ContactPerson.class, "nameSurname", "id",
				access);
		return contactPersons;
	}
	
	
	Object onActivate(Long id) {
		organization = organizationDAO.findByID(id);
		contactPerson = organization.getContactPerson();
		if (organization == null) {
			confirmation.setError("Greska prilikom prikaza zapisa korisnika! ");
			return confirmation;
		}
		return null;
	}

	@CommitAfter
	Object onSuccess() {
		try {
			organization.setContactPerson(contactPerson);
			organizationDAO.update(organization);
			addOrganization.setMsg("Organization succesifuly changed!");
			return addOrganization;
		} catch (Exception e) {
			confirmation.setError("Greska prilikom brisanja zapisa korisnika! ");
			return confirmation;
		}

	}
	
	
}
